function Add-AssemblyToList ($assemblyName, [array]$assemblyList, $biztalkDllNames)
{
	if (-not ($assemblyList -contains $assemblyName))
 	{
		$assemblyFilePath = [System.String]::Concat( $dllPath, "\", $assemblyName.split(",")[0], ".dll")
		if ( [System.IO.File]::Exists($assemblyFilePath) )
		{
			$assembly = [System.Reflection.Assembly]::LoadFrom($assemblyFilePath)
			if ($assembly -ne $null)
			{
				foreach( $childAssembly in $assembly.GetReferencedAssemblies())
				{
					[string]$childFileName = $childAssembly.fullname
					if ( $bizTalkDllNames -contains $childFileName)
					{
						if (-not $assemblyList -contains $childFileName)
						{
							$childFileName = $childAssembly.fullname
							Add-AssemblyToList $childFileName $assemblyList $biztalkDllNames
						}
					}
				} 
			}
		}
	}
	$assemblyList += $assemblyName
	return $assemblyList

}
function Build-AssemblyList($xmlBindings)
{
	$bizTalkDlls = @()
	foreach( $module in $xmlBindings.BindingInfo.ModuleRefCollection.ModuleRef )
	{
		if (-not $module.Name.contains('Application'))
		{
			if ($($module.Name).length -gt 1)
			{
				$modulefullname = $module.fullName
				$bizTalkDlls += $modulefullname
			}
		}
	}
	foreach ($bizTalkDll in $bizTalkDlls)
	{
		[array]$list = Add-AssemblyToList $bizTalkDll $list $bizTalkDlls
	}
	return $list 
}

function Remove-BizTalkAssemblies ($BizTalkApp, [array]$list, $server)
{
	$list = $list[($list.count - 1)..0]
	foreach($item in $list)
	{
		$msg = "Remove biztalk assembly $item"
		if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
		{
			Log-Message $msg  -fore green
			
			if(Is-LocalMachine $server)
			{
				$btsoutput = BTSTask.exe RemoveResource /ApplicationName:$BizTalkApp /LUID:"$item"
			}
			else
			{
				$btsoutput = psexec "\\$server" BTSTask.exe RemoveResource /ApplicationName:$BizTalkApp /LUID:"$item" 2>$null
			}
			$btsoutput | ForEach-Object {log-message $_}
			if ($btsoutput -match "Command failed")
			{
			Throw "Remove BizTalkAssemblies failed"
			}
		}
	}
}

function Add-BizTalkAssemblies($BizTalkApp, $list, $server, $sourcePath)
{
	foreach($item in $list)
	{
		$item = $item.split(",")[0]
		[string]$dllname = $item + ".dll"
		$source = Join-Path $sourcePath $dllname
		$msg = "Adding BizTalk Assembly $item"
		if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
		{
			Log-Message $msg -fore Green
			$cmdstring = "BTSTask.exe AddResource /Source:$source /ApplicationName:$BizTalkApp /Type:BizTalkAssembly /Overwrite /Options:GacOnAdd"
			if(Is-LocalMachine $server)
			{
				$btsoutput = Invoke-Expression $cmdstring
			}
			else
			{
			  $remotecmdstring = "psexec \\$server " + $cmdstring + ' 2>$null'
				$btsoutput = Invoke-Expression $remotecmdstring
			}
			$btsoutput | ForEach-Object {log-message $_}
			if ($btsoutput -match "Command failed")
			{
				Throw "Add BizTalkAssemblies failed"
			}
		}
	}
}
